﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Honestysoft.BaseObject;
using Honestysoft.Financing.Model;
using Honestysoft.Baseframework.Model;
using Honestysoft.Baseframework.WebUI;

namespace Honestysoft.Financing.WebUI.Module.Attach
{
    public partial class ContractFileEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindContract();
                BindContractDocument();
                //dtUser = new BaseBusinessLogic<Sys_User>(Globals.CurrentConnStringName).GetTableByKey(lsUserID.ToArray());
                if (Request.QueryString["ForOpen"] == "1")//打开命令，必定传入项目ID和附件类型
                {
                    //得到附件，如果有则打开
                    QueryParameters query = new QueryParameters();
                    query.Add("DataID", Request.QueryString["ID"], QueryParameters.OperatorType.Equal);
                    query.Add("AttachmentBuType", Request.QueryString["AttachmentBuType"], QueryParameters.OperatorType.Equal);
                    List<Sys_Attachment> lsAtt = new BaseBusinessLogic<Sys_Attachment>(Globals.CurrentConnStringName).GetEntitys(query);
                    if (lsAtt != null && lsAtt.Count == 1)
                    {
                        hidForOpen.Value = Globals.Home + lsAtt[0].Path + "/" + lsAtt[0].FileName + "." + lsAtt[0].Extension;
                    }
                }
            }
        }

        private void BindContract()
        {
            BD_Contract contract = new Honestysoft.Financing.BusinessLogic.Contract(Globals.CurrentConnStringName).GetContract(Request.QueryString["ID"]);
            BD_Project project = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetBaseProject(contract.ProjectID);

            lblOwner.Text = string.IsNullOrEmpty(project.DOwnerName) ? project.OwnerName : ("建设单位：" + project.OwnerName + "<br/>委托代建单位：" + project.DOwnerName);
            lblOwner971.Text = project.Owner971Name;
            lblProjectName.Text = project.Name;
            lblProjectSN.Text = project.SN;
            lblBank.Text = contract.BankName;
            lblBankAccount.Text = contract.BankAccount;
            lblContractName.Text = contract.Name;//<%=string.IsNullOrEmpty(theTable.DBankCompanyName) ? theTable.ContractorName : "施工单位：" + theTable.ContractorName + "<br/>收款单位：" + theTable.DBankCompanyName%></td>
            lblContractorName.Text = string.IsNullOrEmpty(contract.DBankCompanyName) ? contract.ContractorName : ("施工单位：" + contract.ContractorName + "<br/>收款单位：" + contract.DBankCompanyName);
        }

        private void BindContractDocument()
        {
            List<EnumHelper.EnumDataItem> lsEnum = EnumHelper.EnumInstance.GetEnumDataList(typeof(Honestysoft.Financing.Model.EnumItem.ContractDocument));
            List<string> lsValue = new List<string>();
            foreach (EnumHelper.EnumDataItem item in lsEnum)
            {
                lsValue.Add(item.Value);
            }
            QueryParameters query = new QueryParameters();
            query.Add("DataID", Request.QueryString["ID"], QueryParameters.OperatorType.Equal);
            query.Add("AttachmentBuType", lsValue, QueryParameters.OperatorType.IN);
            OrderParameters order = new OrderParameters();
            order.Add("AttachmentBuType");
            List<Sys_Attachment> lsAtt = new BaseBusinessLogic<Sys_Attachment>(Globals.CurrentConnStringName).GetEntitys(query, order);
            List<Sys_Attachment> lsList = new List<Sys_Attachment>();
            Sys_Attachment attItem = null;
            int i = 1;
            foreach (EnumHelper.EnumDataItem item in lsEnum)
            {
                attItem = null;
                if (lsAtt != null && lsAtt.Count > 0)
                {
                    attItem = lsAtt.Find((Sys_Attachment find) => find.AttachmentBuType == item.Value);
                }
                if (attItem == null)
                {
                    attItem = new Sys_Attachment();
                    attItem.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
                    attItem.AttachmentBuType = item.Value;
                    attItem.DataID = Request.QueryString["ID"];
                }
                attItem.Description = i.ToString() + "、" + item.Text;
                i++;
                lsList.Add(attItem);
            }
            rptAtt2.DataSource = lsList;
            rptAtt2.DataBind();
        }

        protected string GetTextUrl(string fileName, string ext, string path)
        {
            string url = "";
            url += "<a href='" + Globals.Home + path + "/" + fileName + "." + ext + "' target='_blank'>";
            url += fileName;
            url += "</a>";
            return url;
        }

        protected string GetIcon(string ext)
        {
            if (string.IsNullOrEmpty(ext))
            {
                return "";
            }
            return "<img src='../../Uploadify/fileType/" + ext + ".gif' border='0' alt='' />";
        }

        protected void btnRebind_Click(object sender, EventArgs e)
        {
            BindContractDocument();
        }
    }
}